﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Bilyteam_Website.Admin
{
    public partial class RelationShip : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable tbGalleries = new DataTable();
        DataTable tbImages = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGalleries();
                BindImages();
            }
        }

        private void BindImages()
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "Select * From Images Where Status = 'True' Order By ImageID Asc";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(ds, "Images");
            tbImages = ds.Tables["Images"];

            if (tbImages != null)
            {
                for (int i = 0; i < tbImages.Rows.Count; i++)
                {
                    chkImages.Items.Add(new ListItem(string.Format("<img src='{0}' alt='' width='100px' height='100px' />", tbImages.Rows[i]["Location"].ToString()), tbImages.Rows[i]["ImageID"].ToString()));
                }
            }
        }

        private void BindGalleries()
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "Select * From Galleries Where Status = 'True' Order By GalleryID Asc";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(ds, "Galleries");
            tbGalleries = ds.Tables["Galleries"];

            dtlGalleries.DataSource = tbGalleries;
            dtlGalleries.DataBind();
        }

        protected void dtlGalleries_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Attributes.Add("onmouseover", "this.oldClass = this.className; this.className = 'EntryLineHover'");
                e.Item.Attributes.Add("onmouseout", "this.className = this.oldClass;");
                e.Item.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(e.Item.Controls[1], string.Empty));
            }
        }

        protected void dtlGalleries_ItemCommand(object source, DataListCommandEventArgs e)
        {
            hdGalleryIDValue.Value = ((HiddenField)e.Item.FindControl("hdGalleryID")).Value;

        }

    }
}